package com.googlecode.tidej.client.project;

import java.io.IOException;

import com.google.gwt.core.client.JavaScriptObject;
import com.googlecode.tidej.client.util.Json;
import com.googlecode.tidej.client.util.LocalStorage;

public final class ProjectMetaData extends JavaScriptObject {
  
  public enum TargetType {
    DEVELOPMENT, DEPLOYMENT
  }

  protected ProjectMetaData() {
  }

  public static native ProjectMetaData create(String name) /*-{
    return {'name':name,'developmentTarget':{},'deploymentTarget':{}};
  }-*/;
  
  private static ProjectMetaData create(String name, String serialized) {
    ProjectMetaData data = (ProjectMetaData) Json.parseToObject(serialized);
    data.setName(name);
    return data;
  }
  
  public native void setName(String name) /*-{
    this['name'] = name;
  }-*/;
  
  public native String getName() /*-{
    return this['name'];
  }-*/;

  public static ProjectMetaData load(String name) {
    try {
      return create(name, LocalStorage.getItem(ProjectManager.STORAGE_PREFIX + name + ".meta"));
    } catch (Exception e) {
     return create(name);
    }
  };

  public String toJson() {
    StringBuilder sb = new StringBuilder();
    char sep = '{';
    for (TargetType targetType: TargetType.values()) {
      sb.append(sep);
      sb.append(targetType == TargetType.DEPLOYMENT ? 
          "deploymentTarget:" : "developmentTarget:");
      sb.append(getTargetData(targetType).toJson());
      sep = ',';
    }
    sb.append('}');
    return sb.toString();
  }

  
  public TargetData getTargetData(TargetType targetType) {
    return getTargetData(targetType == TargetType.DEPLOYMENT ? 
        "deploymentTarget" : "developmentTarget");
  }
  
  private native TargetData getTargetData(String name) /*-{
    var result = this[name];
    if (result == null) {
      result = {};
      this[name] = result;
    }
    return result;
  }-*/;

  public void save() {
    try {
      LocalStorage.setItem(ProjectManager.STORAGE_PREFIX + getName() + ".meta", toJson());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  

  public static final class TargetData extends JavaScriptObject {
    protected TargetData() {
    }
    
    public native String getUrl() /*-{
      return this["url"];
    }-*/;

    public native String getUser() /*-{
      return this["user"];
    }-*/;
    
    public native String getPassword() /*-{
      return this["password"];
    }-*/;
    
    public native void setUrl(String url) /*-{
      this["url"] = url;
    }-*/;

    public native void setUser(String user) /*-{
      this["user"] = user;
    }-*/;

    public native void setPassword(String password) /*-{
      this["password"] = password;
    }-*/;

    public String toJson() {
      StringBuilder sb = new StringBuilder("{");
      sb.append("url:").append(Json.quote(getUrl())).append(',');
      sb.append("user:").append(Json.quote(getUser())).append(',');
      sb.append("password:").append(Json.quote(getPassword()));
      sb.append("}");
      return sb.toString();
    }
  }
}